package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import Boundary.UpdateProject;
import Store.CondReact;
import Store.Event;

public class showProject {
public ShowWelcome root;
showProject self=this;
UpdateProject updateProject;
JPanel pannelloPrincipale;
JPanel centerPan;
JTextArea tipBox = new JTextArea();
JButton classDiagramButton = new JButton("Open Class Diagram", new ImageIcon("src/Images/classDiagram.png"));
JButton StateObserverInvariantsButton = new JButton("Open State Observer / Invariants", new ImageIcon("src/Images/stateObserversInvariants.png"));
JButton EventConditionReactionsButton = new JButton("Open Event / ConditionReactions", new ImageIcon("src/Images/EventConditionsReactions.png"));
JButton StateTableButton = new JButton("Open State Table", new ImageIcon("src/Images/stateTable.png"));
JButton StateMachineButton = new JButton("Generate State Machine", new ImageIcon("src/Images/stateMachine.png"));
JButton CloseButton = new JButton("Close", new ImageIcon("src/Images/close.png"));

public showProject(ShowWelcome root)
{
	this.root=root;
}

public void init(UpdateProject updateProject)
{
	this.updateProject = updateProject;
	display(getPannelloPrincipale(), true);
}

public JPanel getPannelloPrincipale()
{
	pannelloPrincipale = new JPanel();
	pannelloPrincipale.setLayout(new BorderLayout());
	JLabel text = new JLabel("Home - "+updateProject.project.name);
	text.setFont(new Font("Tahoma", Font.BOLD, 32));
	pannelloPrincipale.add(text, BorderLayout.NORTH);
	pannelloPrincipale.add(getCenterPanel(), BorderLayout.CENTER);
	pannelloPrincipale.add(getButtons(), BorderLayout.EAST);
	return pannelloPrincipale;
}

private JScrollPane getCenterPanel() 
{
	JPanel centerPan_aux = new JPanel();
	centerPan_aux.setLayout(new BorderLayout());
	centerPan_aux.add(getStateMachine(), BorderLayout.CENTER);
	centerPan_aux.add(getOutputCD(), BorderLayout.SOUTH);
	JScrollPane scrollPane = new JScrollPane();
	scrollPane.getViewport().add(centerPan_aux);
	
	return scrollPane;
}

private JPanel getStateMachine() 
{
	JPanel stateM = new JPanel();
	JTextArea tA = new JTextArea();
	tA.setFocusable(false);
	tA.setLineWrap(true);
	tA.setText(updateProject.project.SM.getDesc());
	stateM.setLayout(new BorderLayout());
	JLabel l = new JLabel("State Machine :");
	l.setFont(new Font("Tahoma", Font.BOLD, 18));
	stateM.add(l, BorderLayout.NORTH);
	stateM.add(tA, BorderLayout.CENTER);
	return stateM;
}

private JPanel getOutputCD() 
	{
		JPanel panel_aux = new JPanel();
		panel_aux.setLayout(new BorderLayout());
		JTextArea tA= new JTextArea();
		tA.setFocusable(false);
		tA.setLineWrap(true);
		String cd="";
		if(updateProject.project.oCD.getClasses().getAll().size()!=0)
		{
			cd="CLASSES \n";
		for( String c : updateProject.project.oCD.getClasses().getAll())
			cd+=c+"\n\n";
		}
		if(updateProject.project.oCD.getEnums().getAll().size()!=0){
			cd+="\n\nENUMERATION TYPES";
		for(String c: updateProject.project.oCD.getEnums().getAll())
			cd+=c+"\n\n";
		}
		tA.setText(cd);
		JLabel l = new JLabel("Output Class Diagram :");
		l.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel_aux.add(l, BorderLayout.NORTH);
		panel_aux.add(tA);
		return panel_aux;
	}

private JPanel getButtons() 
{
	JPanel container =new JPanel();
	container.setLayout(new GridLayout(6,1));
	container.add(classDiagramButton);
	classDiagramButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	classDiagramButton.setHorizontalTextPosition(AbstractButton.CENTER);
	classDiagramButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {updateProject.openInputCD();}});
	container.add(StateObserverInvariantsButton);
	StateObserverInvariantsButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	StateObserverInvariantsButton.setHorizontalTextPosition(AbstractButton.CENTER);
	StateObserverInvariantsButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {updateProject.openSOINV();}});
	container.add(EventConditionReactionsButton);
	EventConditionReactionsButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	EventConditionReactionsButton.setHorizontalTextPosition(AbstractButton.CENTER);
	EventConditionReactionsButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {updateProject.openEVCR();}});
	container.add(StateTableButton);
	StateTableButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	StateTableButton.setHorizontalTextPosition(AbstractButton.CENTER);
	StateTableButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {updateProject.openST();}});
	container.add(StateMachineButton);
	StateMachineButton.setHorizontalTextPosition(AbstractButton.CENTER);
	StateMachineButton.setVerticalTextPosition(AbstractButton.BOTTOM);	
	StateMachineButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {
			int i=0;
			for (Event aux : updateProject.main.OPEN_PRJ.eventCondReact.events.getAll())
					for( CondReact ctemp: aux.getInfo().getCondReact().getAll())
						if (ctemp.getCondRest()==null && ctemp.getCondUsed()==null && ctemp.getReactRest()==null &&ctemp.getReactUsed()==null)
							i++;
			if(i>0)
				JOptionPane.showMessageDialog(pannelloPrincipale, "Do split cond/react before" , "ERROR", JOptionPane.WARNING_MESSAGE);
			else updateProject.generateSM();}});
	container.add(CloseButton);
	CloseButton.setVerticalTextPosition(AbstractButton.BOTTOM);
	CloseButton.setHorizontalTextPosition(AbstractButton.CENTER);
	CloseButton.addActionListener(new ActionListener() 
		{@Override public void actionPerformed(ActionEvent e) {updateProject.closeProject();}});
	
	return container;
}

public void display(JPanel daVisualizzare, boolean showHome)
{
	if(!showHome)
	{
		root.container.remove(pannelloPrincipale);
		pannelloPrincipale.setVisible(false);
		root.container.revalidate();
		root.container.add(daVisualizzare, BorderLayout.CENTER);
	}
	else
	{
		root.container.removeAll();
		pannelloPrincipale.add(getPannelloPrincipale());
		pannelloPrincipale.setVisible(true);
		root.container.revalidate();
		root.container.add(pannelloPrincipale, BorderLayout.CENTER);
	}
	root.window.repaint();
}

}
